<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="等级名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入等级名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所需成长值" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="growthPoint">
              <a-input-number v-model="model.growthPoint" placeholder="请输入所需成长值" :min="0"
                              style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="免运费标准" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="freeFreightPoint">
              <a-input-number v-model="model.freeFreightPoint" placeholder="请输入免运费标准" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="评价成长值" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="commentGrowthPoint">
              <a-input-number :min="0" v-model="model.commentGrowthPoint" placeholder="请输入每次评价获取的成长值"
                              style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="默认等级" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="defaultStatus">
              <z-switch v-model="model.defaultStatus" :un-checked="'否'" :checked="'是'"></z-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="免邮特权" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="priviledgeFreeFreight">
              <z-switch v-model="model.priviledgeFreeFreight" :un-checked="'否'" :checked="'是'"></z-switch>

            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="会员价格特权" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="priviledgeMemberPrice">
              <z-switch v-model="model.priviledgeMemberPrice" :un-checked="'否'" :checked="'是'"></z-switch>

            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生日特权" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="priviledgeBirthday">
              <z-switch v-model="model.priviledgeBirthday" :un-checked="'否'" :checked="'是'"></z-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="note">
              <a-input v-model="model.note" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'
import ZSwitch from '../../../components/compents/ZSwitch.vue'
import { postAction } from '../../../api/manage'

export default {
  name: 'UmsMemberLevelForm',
  components: { ZSwitch },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        name: '',
        growthPoint: '',
        defaultStatus: 0,
        freeFreightPoint: '',
        commentGrowthPoint: '',
        priviledgeFreeFreight: 0,
        priviledgeMemberPrice: 0,
        priviledgeBirthday: 0,
        note: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, validator: this.verifyLevelName }],
        growthPoint: [{ required: true, message: '请输入等级所需成长值！' }],
        defaultStatus: [{ required: true, message: '请选择是否为默认等级！' }],
        freeFreightPoint: [{ required: true, message: '输入免运费标准！' }],
        commentGrowthPoint: [{ required: true, message: '输入每次评价获取的成长值！' }],
        priviledgeFreeFreight: [{ required: true, message: '请选择是否有免邮特权！' }],
        priviledgeMemberPrice: [{ required: true, message: '请选择是否有会员特权！' }],
        priviledgeBirthday: [{ required: true, message: '请选择是否有生日特权！！' }]
      },
      url: {
        add: '/member/memberLevel/add',
        edit: '/member/memberLevel/edit',
        queryById: '/member/memberLevel/queryById',
        verifyLevelName: '/member/memberLevel/verifyLevelName'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    },
    /**
     * 校验属性名称是否重复
     */
    verifyLevelName(rule, value, callback) {
      if (value === undefined || value === '') {
        return callback(new Error('请输入等级名称!'))
      }
      postAction(this.url.verifyLevelName, this.model).then(res => {
        if (res.success) {
          callback()
        } else {
          return callback(new Error(res.message))
        }
      })

    }
  }
}
</script>